package com.java.main.controller;

import java.io.FileNotFoundException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.java.main.constant.Constants;
import com.java.main.constant.Errors;
import com.java.main.exception.HiBabyGlobalException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.main.controller.output.BaseOutput;
import com.java.main.controller.output.ResultResponseBeanUtil;
import com.java.main.domain.KbmContent;
//import com.hibaby.crm.util.JedisUtil;

@RequestMapping(value = "/crm/api/v1")
public class BaseController {
	private Logger log = LoggerFactory.getLogger(getClass());

	@ResponseBody
	@ExceptionHandler
	public BaseOutput<Void> exp(HttpServletRequest request, Exception ex) {
		log.error(ex.getMessage());
		request.setAttribute("ex", ex);
		log.error("*********************************");
		log.error("*********************************");
		log.error(ex.getMessage());
		log.error("*********************************");
		log.error("*********************************");
		ex.printStackTrace();
		if (ex instanceof HiBabyGlobalException) {
			HiBabyGlobalException be = (HiBabyGlobalException) ex;

			Errors errors = be.getErrors();
			if (errors == null) {
				errors = Errors.SYSTEM_ERROR;
			}
			return ResultResponseBeanUtil.fail(errors, ex.getMessage());
		} else if (ex instanceof FileNotFoundException) {
			return ResultResponseBeanUtil.fail(Errors.SYSTEM_ERROR, ex.getMessage());
		} else if (ex instanceof MethodArgumentNotValidException) {
			StringBuilder errorMessage = new StringBuilder();
			try {
				MethodArgumentNotValidException mex = (MethodArgumentNotValidException) ex;
				BindingResult br = mex.getBindingResult();
				List<FieldError> fieldErrorList = br.getFieldErrors();
				if (fieldErrorList.isEmpty()) {
					errorMessage = new StringBuilder(ex.getMessage());
				}
				for (FieldError fieldError : fieldErrorList) {
					errorMessage.append(fieldError.getObjectName());
					errorMessage.append(".");
					errorMessage.append(fieldError.getField());
					errorMessage.append(":");
					errorMessage.append(fieldError.getDefaultMessage());
					errorMessage.append(";");
				}
			} catch (Exception e) {
			}
			return ResultResponseBeanUtil.fail(Errors.VALID_ERROR, errorMessage.toString());
		} else {
			ex.printStackTrace();
			return ResultResponseBeanUtil.fail(Errors.SYSTEM_ERROR);
		}
	}

	public KbmContent getCurrentInfo(HttpServletRequest request) {
		KbmContent result = (KbmContent) request.getSession().getAttribute(Constants.CURRENT_SYS_USER_INFO);
		if (result == null) {
			throw new HiBabyGlobalException(Errors.NOT_LOGIN);
		}
		return result;
	}

	/**
	 * 更新session中的上下文信息并更新redis中的信息
	 * 
	 * @param request
	 * @param kbmContent
	 */
	public void setCurrentInfo(HttpServletRequest request, KbmContent kbmContent) {
//		JedisUtil.setObj(Constants.CURRENT_SYS_USER_TOKEN + kbmContent.getToken(), kbmContent);
		request.getSession().setAttribute(Constants.CURRENT_SYS_USER_INFO, kbmContent);
	}

	/**
	 * 获取当前登录的C端用户信息
	 * @param request
	 * @return
	 */
	public KbmContent getCustomerUserCurrentInfo(HttpServletRequest request) {
		KbmContent result = (KbmContent) request.getSession().getAttribute(Constants.CURRENT_CUSTOMER_USER_INFO );
		if (result == null) {
			throw new HiBabyGlobalException(Errors.NOT_LOGIN);
		}
		return result;
	}
}
